package com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.model.Commodity;
import com.model.User;
import com.service.CommodityService;
import com.service.UserService;

@RestController
public class userController {
	@Autowired
	UserService ud;
	@Autowired
	CommodityService cs;
	@RequestMapping("login1")
	public Map findUser(@RequestParam("name") String userName,@RequestParam("password") String pwd){
		Map r=new HashMap();
		//System.out.println(U1);
		String m;
		if(null!=ud.selectByName(userName)){
			User U1=ud.selectByName(userName);
			if(U1.getUserpwd().equals(pwd)){
				m="Y";
				
				
			}else{
				m="S";
			}
			
		}else{
			m="N";
		}
		
		r.put("rs", m);
		return r;
		}
	
	@RequestMapping(value="/register1",method=RequestMethod.POST) 
	public Map register1(@RequestParam("name") String userName,@RequestParam("password") String pwd){
		System.out.println("userName"+userName+"--pwd--"+pwd+"email---");		
		Map m=new HashMap();
		
		Map r=new HashMap();
		User u=new User();
		u.setUsername(userName);
		u.setUserpwd(pwd);
		
		
		
		String fq;
		if(null==ud.selectByName(userName)){
			ud.insertSelective(u);
			 fq="Y";
		}else{
			 fq="N";
		}
		r.put("rs", fq);
		return r;
		}
		
	@RequestMapping(value="/getGoods",method=RequestMethod.POST)
	public List<Commodity> getGoods1(){
		return cs.selectAll();
	}
	
	@RequestMapping(value="/deletGoods",method=RequestMethod.POST)
	public Map deletGoods1(@RequestParam("id") int id){
		Map m=new HashMap();
		if(0<cs.deleteByPrimaryKey(id)){
		m.put("rs", "Y");
		m.put("rg", cs.selectAll());
		}else{
			m.put("rs", "N");
			m.put("rg", cs.selectAll());
		}
		return m;
	}
	
	@RequestMapping(value="/updateName",method=RequestMethod.POST)
	public Map updateName1(@RequestParam("id") int id,@RequestParam("cName")String cName){
		Map m=new HashMap();
		System.out.println("START");
		Commodity c=new Commodity();
		c.setcId(id);
		c.setCname(cName);
		if(0<cs.updateByPrimaryKeySelective(c)){
		m.put("rs", "Y");
		m.put("rg", cs.selectAll());
		}else{
			m.put("rs", "N");
			m.put("rg", cs.selectAll());
		}
		return m;
	}
	
	@RequestMapping(value="/findName1",method=RequestMethod.POST)
	public Map findName1(@RequestParam("cName")String cName){
		Map m=new HashMap();
		if(0<cs.selectByCommodityName(cName).size()){
		m.put("rs", "Y");
		m.put("rg", cs.selectByCommodityName(cName));
		}else{
			m.put("rs", "N");
			m.put("rg", cs.selectAll());
		}
		return m;
	}
	@RequestMapping(value="/addName1",method=RequestMethod.POST)
	public Map addName1(@RequestParam("cName")String cName){
		Map m=new HashMap();
		Commodity c=new Commodity();
		
		c.setCname(cName);
		if(0<cs.insertSelective(c)){
		m.put("rs", "Y");
		m.put("rg", cs.selectAll());
		}else{
			m.put("rs", "N");
			m.put("rg", cs.selectAll());
		}
		return m;
	}
}
